package controllers

import dao._
import play.api.i18n.I18nSupport
import play.api.mvc.{AbstractController, ControllerComponents}
import shared.Pojo.VarInfoDataKind
import shared.VarTool
import tool.Tool

import java.io.File
import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import implicits.Implicits._

/**
 * Created by yz on 6/1/2021
 */
class UpgradeController @Inject()(cc: ControllerComponents)(
  implicit val varInfoFileDataDao: VarInfoFileDataDao
) extends AbstractController(cc) with I18nSupport {

  def varInfoFillColumnNum = Action.async { implicit request =>
    varInfoFileDataDao.selectAll.flatMap { varInfoFiles =>
      val newVarInfoFiles = varInfoFiles.map { varInfoFile =>
        val kindDir = Tool.getUserDataKindDir(varInfoFile.userId, VarInfoDataKind.name)
        val file = new File(kindDir, varInfoFile.fileName)
        val columnNum = file.txtLines.head.size
        varInfoFile.copy(columnNum = columnNum)
      }
      varInfoFileDataDao.updates(newVarInfoFiles)
    }.map{x=>
      Ok("upgrade success!")
    }

  }

}
